#include "portstatistics.h"
#include <QDebug>

PortStatistics::PortStatistics(QObject *parent)
    : QObject (parent)
{
    init();
}

PortStatistics::~PortStatistics()
{

}

void PortStatistics::init()
{
    m_current.m_receivedTotalBytes = 0;
    m_current.m_receivedCorrectBytes = 0;
    m_current.m_receivedErrorPackets = 0;
    m_current.m_receivedCorrectPackets = 0;

    m_receivedBytesPerSecond = 0;
    m_receivedCorrectBytesPerSecond = 0;
    m_receivedErrorPacketsPerSecond = 0;
    m_receivedCorrectPacketsPerSecond = 0;

    m_current.m_sendTotalBytes = 0;
    m_current.m_sendPackets = 0;

    m_sendBytesPerSecond = 0;
    m_sendPacketsPerSecond = 0;
};

void PortStatistics::on1sTimeout()
{
//    m_statisticsInfo[0].receivedErrorPackets = mParser->getErrorPacketCounts();

    m_receivedBytesPerSecond = m_current.m_receivedTotalBytes - m_last.m_receivedTotalBytes;
    m_receivedCorrectBytesPerSecond = m_current.m_receivedCorrectBytes - m_last.m_receivedCorrectBytes;
    m_receivedErrorPacketsPerSecond = m_current.m_receivedErrorPackets - m_last.m_receivedErrorPackets;
    m_receivedCorrectPacketsPerSecond = m_current.m_receivedCorrectPackets - m_last.m_receivedCorrectPackets;
    m_sendPacketsPerSecond = m_current.m_sendPackets - m_last.m_sendPackets;
    m_sendBytesPerSecond = m_current.m_sendTotalBytes - m_last.m_sendTotalBytes;
    m_last = m_current;

//    serial_icounter_struct counter;
//    if (ioctl(mFd, TIOCGICOUNT, &counter) != -1 && mStatisticsInfo[0].serial.type)
//    {
//        mStatisticsInfo[0].serial.overrun = counter.overrun;
//    }

}
void PortStatistics::print()
{
    // debug statistics infomation
    qDebug() << "serial port statistics infomation\n"
             << "receive bytes:" << m_current.m_receivedTotalBytes << "\n"
             << "receive correct bytes:" << m_current.m_receivedCorrectBytes << "\n"
             << "receive error packets:" << m_current.m_receivedErrorPackets << "\n"
             << "receive correct packets:" << m_current.m_receivedCorrectPackets << "\n"
             << "receive bytes 1s:" << m_receivedBytesPerSecond << "\n"
             << "receive correct bytes 1s:" << m_receivedCorrectBytesPerSecond << "\n"
             << "receive error packets 1s:" << m_receivedErrorPacketsPerSecond << "\n"
             << "receive correct packets 1s:" << m_receivedCorrectPacketsPerSecond << "\n"
             << "send bytes:" << m_current.m_sendTotalBytes << "\n"
             << "send packets:" << m_current.m_sendPackets << "\n"
             << "send bytes 1s:" << m_sendBytesPerSecond << "\n"
             << "send packets 1s:" << m_sendPacketsPerSecond << "\n"
//             << "overrun count:" << m_serial.overrun;
                ;
}

void PortStatistics::sendBytes(int bytes)
{
    m_current.m_sendTotalBytes += bytes;
    m_current.m_sendPackets++;
}

void PortStatistics::receiveBytes(int bytes)
{
    m_current.m_receivedTotalBytes += bytes;
}

void PortStatistics::receiveCorrectBytes(int bytes)
{
    m_current.m_receivedCorrectBytes += bytes;
    m_current.m_receivedCorrectPackets++;
}
